<template>
    <div class="bac">
        <!-- 封装的load组件，用法：当当前页面数据已经请求回来的时候show的值动态传个true，没回来的时候动态传false -->
        <load :show="city.length == 0?false:true"></load>
        <div v-if="city.length !== 0">
            <title-a>
                选择所在地区
            </title-a>
            <div class="ba">
                当前定位：<span>{{city_name}}</span>
            </div>
            <ul class="ul">
                <li class="first">请选择<span>省市/地区</span></li>
                <li v-for="item in city" :key="item.province_id" @click="next(item.province_id)">{{item.province_name}}</li>
            </ul>
        </div>
    </div>
</template>
<script>
import Vue from 'vue'
import axios from 'axios'
import { mapState, mapMutations } from 'vuex'
export default {
  data () {
    return {
      city: [],
      ci: ''
    }
  },
  mounted () {
    // 设置axios为form-data
    axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
    axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded'
    axios.defaults.transformRequest = [function (data) {
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }]
    // 然后再修改原型链
    Vue.prototype.$axios = axios

    axios.post('lct?api_version=2.3.0&platType=2&client=wap&isEncry=0&time=1583473320787&act=Address&op=get_region', {
      region: 'province_name'
    }).then(res => {
      console.log(111, res.data)
      this.city = res.data.datas
    })

    this.isshow()
  },
  methods: {
    ...mapMutations(['isshow', 'ishide']),
    next (id) {
      // console.log(id);
      this.$router.push(`/address/city/${id}`)
    }
  },
  computed: {
    ...mapState(['city_name'])
  },
  destroyed () {
    this.ishide()
  }
}
</script>
<style lang="scss" scoped>
    .ba{
        height: 30x;
        display: flex;
        font-size: 16px;
        background: rgb(233, 232, 232);
        margin-top:50px;
        // justify-content: center;
        align-items: center;
        padding:10px;
        color: rgb(0, 0, 0);
        span{
            font-size: 20px;
        }
    }
    .ul{
        .first{
            font-size:16px;
            color: rgb(165, 164, 164);
            span{
                font-size: 18px;
                color: rgb(26, 26, 26);
                margin-left: 20px;
            }
        }
        li{
            height:30px;
            padding: 10px;
            line-height: 30px;
            font-size: 18px;
            border-bottom: 1px solid #ccc;
        }
    }
</style>
